import { list as listCategory } from '../services/category';
import { search as searchArticle, total } from '../services/article';


export default {
  namespace: 'article',

  state: {
    articleList: [],
    articleTotal: 0,
    categoryList: [],
    total: 0,
  },


  effects: {
    *listCategory(_, { call, put }) {
      const response = yield call(listCategory);
      yield put({
        type: 'loadCategoryList',
        payload: response.data,
      });
    },
    *total(_, { call, put }){
      const response = yield call(total);
      yield put({
        type: 'loadTotal',
        payload: response.data,
      });
    },
    *listArticle({ payload }, { call, put }){
      const response = yield call(searchArticle, payload);
      yield put({
        type: 'loadArticleList',
        payload: response.data,
      });
    },
  },

  reducers: {
    loadCategoryList(state, action) {
      return {
        ...state,
        categoryList: action.payload,
      }
    },
    loadArticleList(state, action){
      return {
        ...state,
        articleList: action.payload.list,
        articleTotal: action.payload.total,
      }
    },
    loadTotal(state, action){
      return {
        ...state,
        total: action.payload.total,
      }
    },
  },
}
